package net.zjitc.servlet;

import net.zjitc.domain.User;
import net.zjitc.service.UserService;
import net.zjitc.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

/**
 * @Author admin
 * @Date 2019/3/4 10:48
 */
@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String username = request.getParameter("username");
        String password = request.getParameter("password");
        UserService service = new UserServiceImpl();
        User u = service.findByUsernameAndPassword(username,password);

            if(u != null){
                HttpSession session = request.getSession();
                session.setAttribute("user",u);
                String path = request.getContextPath();
                response.sendRedirect(path+"/findDetail");
            }
            else{
                request.setAttribute("msg","用户名或密码错误！");
                request.getRequestDispatcher("/login.jsp").forward(request,response);
            }



    }
}
